package com.jynn.mesh.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.jynn.mesh.service.FileDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

@RestController
public class FileDownloadController {

    @Autowired
    private FileDownloadService fileDownloadService;
    
    @RequestMapping(value = "/download")
    public void download(HttpServletResponse response) throws Exception {
    	try {
	    	String fileUrl = "https://git.eclipse.org/r/plugins/gitiles/mdmbl/org.eclipse.mdm/+/refs/heads/dev/LICENSE.txt";
	
	    	//将文件下载后保存在E盘，返回结果为下载文件大小
	    	long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("/Users/liuwenxu/Documents/preview/"));
	    	System.out.println("Download size: " + size);
    	}catch(Exception e) {
    		System.out.println(e.getMessage());
    	}

    }

    /**
     * 下载文件
     *
     * @param name
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/download/{name}")
    public void logDownload(@PathVariable String name, HttpServletResponse response) throws Exception {
    	String filePath = "/Users/liuwenxu/Documents/";
		String fileNamess = "MapleTR4J安装部署_V1.0_20200724.docx";
        boolean downfile = fileDownloadService.fileDownload(filePath, fileNamess, response);
//        if(downfile==true) {
//        	String outPath = filePath+"preview/";
//        	String htmlName = "rand.html";
//        	String outPutFile = outPath+htmlName;
//        	String[] htmlNameArr = fileNamess.split("\\.");
//        	//判断文件类型
//			if ("doc".equals(htmlNameArr[htmlNameArr.length-1])) {
//				FileHandleUtils.docToHtml(outPath, outPath+fileNamess, outPutFile);
//			} else if ("docx".equals(htmlNameArr[htmlNameArr.length-1])) {
//				FileHandleUtils.docxToHtml(outPath, outPath+fileNamess, outPutFile);
//			} else if ("xls".equals(htmlNameArr[htmlNameArr.length-1])) {
//				FileHandleUtils.HSSFToHtml(outPath, outPath+fileNamess, outPutFile);
//			} else if ("xlsx".equals(htmlNameArr[htmlNameArr.length-1])) {
//				ExcelToHtml.excelToHtml(outPath+fileNamess, outPutFile);
//			} else if ("pdf".equals(htmlNameArr[htmlNameArr.length-1])) {
//				FileCopyUtils.copy(new FileInputStream(outPath+fileNamess), response.getOutputStream());
//			} else {
//				response.setContentType("text/html;charset=utf-8");
//				PrintWriter out = response.getWriter();
//				String message = "<div style='width:100%;text-align:center;color:red;'>抱歉！当前文件格式不支持预览</div>";
//				out.write(message);
//				out.close();
//				return;
//			}
//			File outFile = new File(outPutFile);
//			if(outFile.exists()!=false) {
//				//将生成的html文件返回给前端页面
//				FileCopyUtils.copy(new FileInputStream(outPutFile), response.getOutputStream());
//			}
//			delAllFile(outPath);
//        }
    }
    
  //删除文件夹  
  	private static void delFolder(String folderPath) {  
  	     try {  
  	        delAllFile(folderPath); //删除完里面所有内容  
  	        String filePath = folderPath;  
  	        filePath = filePath.toString();  
  	        java.io.File myFilePath = new java.io.File(filePath);  
  	        myFilePath.delete(); //删除空文件夹  
  	     } catch (Exception e) {  
  	       e.printStackTrace();   
  	     }  
  	}  
  	
  	//删除指定文件夹下的所有文件
  	private static boolean delAllFile(String path) {  
       boolean flag = false;  
       File file = new File(path);  
       if (!file.exists()) {  
         return flag;  
       }  
       if (!file.isDirectory()) {  
         return flag;  
       }  
       String[] tempList = file.list();  
       File temp = null;  
       for (int i = 0; i < tempList.length; i++) {  
          if (path.endsWith(File.separator)) {  
             temp = new File(path + tempList[i]);  
          } else {  
              temp = new File(path + File.separator + tempList[i]);  
          }  
          if (temp.isFile()) {  
             temp.delete();  
          }  
          if (temp.isDirectory()) {  
             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件  
             delFolder(path + "/" + tempList[i]);//再删除空文件夹
             flag = true;  
          }  
       }  
       return flag;  
   }
    
    /**
     * 上传日志接口
     *
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload")
    public void logUpload(@RequestParam("file") MultipartFile file) throws Exception {
        fileDownloadService.fileUpload(file);
    }
}
